<?php

/**
 * Yii Controller SearchController
 *
 * @author hoangdung
 * @since Jan 23, 2014 - 4:03:19 PM
 */
class SearchController extends MyController
{

    private $arr_id_customer = array();

    //dung cho customer
    private function criteria()
    {
        $key = $_POST['key'];

        $criteria = new CDbCriteria();

        //if admin return all customer
        $criteria->compare('firstname', $key, TRUE, 'OR');
        $criteria->compare('lastname', $key, TRUE, 'OR');
        $criteria->compare('mobile_phone', $key, TRUE, 'OR');
        $criteria->compare('type', Users::TYPE_CUSTOMER);

        $criteria->select = array('firstname', 'lastname', 'id', 'mobile_phone', 'email', 'photo', 'type');

        return $criteria;
    }

    /**
     * @return mixed retrive list of customer's id or <b>Empty array</b> if not found
     */
    private function get_list_user_id()
    {

        $list_id_branh = Users::get_all_branch_id_of_user_logedin();

        if (!empty($list_id_branh))
        {

            $crite        = new CDbCriteria();
            $crite->addInCondition('biz_id', $list_id_branh);
            $crite->group = 'user_id';

            $customer = Customer::model()->findAll($crite);

            if (NULL != $customer)
            {

                $arr_reponse     = array();
                $arr_id_customer = array();

                foreach ($customer as $c)
                {
                    $arr_reponse[]     = $c->user_id;
                    $arr_id_customer[] = $c->id;
                }

                $this->arr_id_customer = $arr_id_customer;

                return $arr_reponse;
            }
            else
            {
                return array();
            }
        }
        else
        {
            return array();
        }
    }

    /**
     * @return array array of user or <b>"[]"</b> if empty
     */
    public function actionGetListSuggestUser()
    {

        if ($_POST['key'] && $_POST['key'] != '')
        {

            if (Yii::app()->user->isConcierge() OR Yii::app()->user->isAdmin())
            {
                $cri = $this->criteria();

                $cus = Users::model()->findAll($cri);

                $this->respon_browser($cus, TRUE);
            }

            $list_id_user = $this->get_list_user_id();

            if (empty($list_id_user))
            {
                echo json_encode(array());
                Yii::app()->end();
            }

            $cri = $this->criteria();

            //if user loged is not admin
            if (FALSE == Yii::app()->user->isAdmin())
            {
                $cri->addInCondition('id', $list_id_user);
            }

            $cus = Users::model()->findAll($cri);

            $this->respon_browser($cus);
        }
        else
        {
            echo json_encode(array());
            Yii::app()->end();
        }
    }

    /**
     * 
     * @param mixed $cus_active_record list of Active record of Customer
     */
    private function respon_browser($cus_active_record, $is_belong_merchant = FALSE)
    {
        if ($cus_active_record == NULL)
        {
            echo json_encode(array());
            Yii::app()->end();
        }
        else
        {
            $arr = array();

            foreach ($cus_active_record as $v)
            {

                $cus_merchant = $v->customers;

                if (NULL != $cus_merchant)
                {

                    foreach ($cus_merchant as $cm)
                    {

                        if ($is_belong_merchant)
                        {
                            $this->_echo_json($v, $cm, $arr);
                        }
                        //if admin, no need
                        if (in_array($cm->id, $this->arr_id_customer))
                        {
                            $this->_echo_json($v, $cm, $arr);
                        }
                    }
                }
            }
            echo json_encode($arr);
            Yii::app()->end();
        }
    }

    private function _echo_json($customer_ar, $customer_biz_ar, &$arr)
    {
        $arr[] = array(
            'cus_id'       => $customer_ar->id,
            'lastname'     => $customer_ar->lastname,
            'firstname'    => $customer_ar->firstname,
            'viewUrl'      => $customer_biz_ar->viewUrl,
            'email'        => $customer_ar->email,
            'merchant'     => ($customer_biz_ar->biz) ? @$customer_biz_ar->biz->business_name : 'webmaster',
            'photo'        => $customer_ar->getAvartBySize(69, 69),
            'mobile_phone' => ($customer_ar->mobile_phone) ? $customer_ar->mobile_phone : '',
        );
    }

}

/** 
 * End file SearchController
 */
